﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;

namespace KCommon
{
    public class ConfigReader
    {
        public static bool ReaderLayoutConfig(string appName, List<Dictionary<string, string>> controlDicList)
        {
            //Log.WriteAsync(appName + "：开始读配置");
            try
            {
                //配置文件路径
                string codeBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string pathConfig = codeBase + "\\Config\\" + appName + ".xml";
                if (!File.Exists(pathConfig))
                {
                    //Log.WriteAsync("不存在路径：" + pathConfig);
                    pathConfig = Tools.GetAssemblyPath() + "..//Config//" + appName + ".xml";
                    if (!File.Exists(pathConfig))
                    {
                        //Log.WriteAsync("不存在路径：" + pathConfig);
                        return false;
                    }
                }
                XmlDocument doc = new XmlDocument();
                //Log.WriteAsync("配置路径：" + pathConfig);
                doc.Load(pathConfig);
                XmlNode winNode = doc.SelectSingleNode("Windows");
               
                foreach (XmlNode xns in winNode)
                {
                    if (xns is XmlComment)  //跳过注释
                    {
                        continue;
                    }

                    Dictionary<string,string> controlAtr = new Dictionary<string, string>();
                    controlAtr.Add("ControlType", xns.Name);
                    foreach (XmlAttribute xa in ((XmlElement)xns).Attributes)
                    {
                        controlAtr.Add(xa.Name,xa.Value);
                    }
                    controlDicList.Add(controlAtr);
                }
            }
            catch
            {
               // Log.WriteAsync("异常：" +  e.ToString());
                throw;
            }
            //Log.WriteAsync(appName + "：结束读配置");
            return true;
        }

        //public static bool ReaderPlugin(string appName, List<Dictionary<string, string>> controlDicList)
        //{
        //    Log.WriteAsync(appName + "：开始读配置");
        //    try
        //    {
                
        //        //配置文件路径
        //        string pathConfig = new DirectoryInfo(Tools.GetAssemblyPath()).Parent + "\\Config\\" + appName + ".xml";
        //        if (!File.Exists(pathConfig))
        //        {
        //            //pathConfig = new DirectoryInfo(Tools.GetAssemblyPath()).Parent.FullName + "//Config//" + appName + ".xml";
        //            pathConfig = Tools.GetAssemblyPath() + "..//Config//" + appName + ".xml";
        //            if (!File.Exists(pathConfig))
        //            {
        //                Log.WriteAsync("不存在路径：" + pathConfig);
        //                return false;
        //            }
        //        }
        //        XmlDocument doc = new XmlDocument();
        //        Log.WriteAsync("配置路径：" + pathConfig);
        //        doc.Load(pathConfig);
        //        XmlNode winNode = doc.SelectSingleNode("Windows");
        //        winNode = winNode.SelectSingleNode("Plugins");
        //        foreach (XmlNode xns in winNode)
        //        {
        //            if (xns is XmlComment)  //跳过注释
        //            {
        //                continue;
        //            }

        //            Dictionary<string, string> controlAtr = new Dictionary<string, string>();
        //            //controlAtr.Add("ControlType", xns.Name);
        //            foreach (XmlAttribute xa in ((XmlElement)xns).Attributes)
        //            {
        //                controlAtr.Add(xa.Name, xa.Value);
        //            }
        //            controlDicList.Add(controlAtr);
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        Log.WriteAsync("异常：" + e.ToString());
        //        throw;
        //    }
        //    Log.WriteAsync(appName + "：结束读配置");
        //    return true;
        //}

        public static bool ReaderModule(string appName, List<Dictionary<string, string>> moduleDicList)
        {
            //Log.WriteAsync(appName + "：开始读配置");
            try
            {
                string codeBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                //配置文件路径
                string pathConfig = codeBase + "\\Config\\" + appName + ".xml";
                if (!File.Exists(pathConfig))
                {
                    //pathConfig = new DirectoryInfo(Tools.GetAssemblyPath()).Parent.FullName + "//Config//" + appName + ".xml";
                    pathConfig = Tools.GetAssemblyPath() + "..//Config//" + appName + ".xml";
                    if (!File.Exists(pathConfig))
                    {
                       // Log.WriteAsync("不存在路径：" + pathConfig);
                        return false;
                    }
                }
                XmlDocument doc = new XmlDocument();
                //Log.WriteAsync("配置路径：" + pathConfig);
                doc.Load(pathConfig);
                XmlNode winNode = doc.SelectSingleNode("Windows");
                winNode = winNode.SelectSingleNode("Modules");
                foreach (XmlNode xns in winNode)
                {
                    if (xns is XmlComment)  //跳过注释
                    {
                        continue;
                    }

                    Dictionary<string, string> controlAtr = new Dictionary<string, string>();
                    //controlAtr.Add("ControlType", xns.Name);
                    foreach (XmlAttribute xa in ((XmlElement)xns).Attributes)
                    {
                        controlAtr.Add(xa.Name, xa.Value);
                    }
                    moduleDicList.Add(controlAtr);
                }
            }
            catch
            {
                //Log.WriteAsync("异常：" + e.ToString());
                throw;
            }
            //Log.WriteAsync(appName + "：结束读配置");
            return true;
        }

        public static bool ReaderPluginButton(string appName, List<Dictionary<string, string>> moduleDicList)
        {
            //Log.WriteAsync(appName + "：开始读配置");
            try
            {
                //配置文件路径
                string pathConfig = new DirectoryInfo(Tools.GetAssemblyPath()).Parent + "\\Config\\" + appName + ".xml";
                if (!File.Exists(pathConfig))
                {
                    //pathConfig = new DirectoryInfo(Tools.GetAssemblyPath()).Parent.FullName + "//Config//" + appName + ".xml";
                    pathConfig = Tools.GetAssemblyPath() + "..//Config//" + appName + ".xml";
                    if (!File.Exists(pathConfig))
                    {
                        //Log.WriteAsync("不存在路径：" + pathConfig);
                        return false;
                    }
                }
                XmlDocument doc = new XmlDocument();
                //Log.WriteAsync("配置路径：" + pathConfig);
                doc.Load(pathConfig);
                XmlNode winNode = doc.SelectSingleNode("Windows");
                winNode = winNode.SelectSingleNode("BaseButtons");
                foreach (XmlNode xns in winNode)
                {
                    if (xns is XmlComment)  //跳过注释
                    {
                        continue;
                    }

                    Dictionary<string, string> controlAtr = new Dictionary<string, string>();
                    //controlAtr.Add("ControlType", xns.Name);
                    foreach (XmlAttribute xa in ((XmlElement)xns).Attributes)
                    {
                        controlAtr.Add(xa.Name, xa.Value);
                    }
                    moduleDicList.Add(controlAtr);
                }
            }
            catch
            {
                //Log.WriteAsync("异常：" + e.ToString());
                throw;
            }
            //Log.WriteAsync(appName + "：结束读配置");
            return true;
        }

        public static bool ReaderDeviceCfg(List<Dictionary<string, string>> deviceList)
        {
            //Log.WriteAsync("开始读配置");
            try
            {
                //配置文件路径
                string pathConfig = Tools.GetAssemblyPath() + "\\Config\\" + "Device.xml";
                if (!File.Exists(pathConfig))
                {
                    //pathConfig = new DirectoryInfo(Tools.GetAssemblyPath()).Parent.FullName + "//Config//" + appName + ".xml";
                    pathConfig = Tools.GetAssemblyPath() + "..//Config//" + "Device.xml";
                    if (!File.Exists(pathConfig))
                    {
                        //Log.WriteAsync("不存在路径：" + pathConfig);
                        return false;
                    }
                }
                XmlDocument doc = new XmlDocument();
                //Log.WriteAsync("配置路径：" + pathConfig);
                doc.Load(pathConfig);
                XmlNode winNode = doc.SelectSingleNode("Devices");
                 //winNode = winNode.SelectSingleNode("Device");
                foreach (XmlNode xns in winNode)
                {
                    if (xns is XmlComment)  //跳过注释
                    {
                        continue;
                    }

                    Dictionary<string, string> controlAtr = new Dictionary<string, string>();
                    //controlAtr.Add("ControlType", xns.Name);
                    foreach (XmlAttribute xa in ((XmlElement)xns).Attributes)
                    {
                        controlAtr.Add(xa.Name, xa.Value);
                    }
                    deviceList.Add(controlAtr);
                }
            }
            catch
            {
               // Log.WriteAsync("异常：" + e.ToString());
                throw;
            }
            //Log.WriteAsync("结束读配置");
            return true;
        }
    }
}
